﻿using System.Linq;
using Microsoft.SharePoint.Administration;
using Trentacular.SharePoint.Deployment.Model;
using Trentacular.SharePoint.Deployment.Util;
using Trentacular.SharePoint.WSPUtil;

namespace Trentacular.SharePoint.Deployment.Data.Proxy
{
    public class SolutionFeatureDefinitionsLazyInitializer : BaseDomainListLazyInitializer<Solution, FeatureDefinition>
    {
        protected override object InitializeTarget()
        {
            var spFarm = SPFarm.Local;
            var spSolution = spFarm.Solutions[Owner.Id];

            return spSolution.ExtractFeatureDefinitions()
                .Select(fd => AdaptFeatureDefinition(fd))
                .ToVirtualList();
        }

        private FeatureDefinition AdaptFeatureDefinition(WSPFeatureDefinition wspFeatureDefinition)
        {
            return new FeatureDefinition
            {
                Solution = Owner,
                Id = wspFeatureDefinition.Id,
                Name = wspFeatureDefinition.Name,
                Scope = wspFeatureDefinition.Scope,
                Title = wspFeatureDefinition.Title,
                Description = wspFeatureDefinition.Description
            };
        }
    }
}
